package com.burgeon.backend.EnterpriseWeChat.rest.Analysis;

import com.burgeon.backend.EnterpriseWeChat.entity.VipData;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 购物车推荐
 */
public class getConstellation extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public getConstellation(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<VipData>  getData(String storeId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(storeId), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<VipData> vList= new ArrayList<>();

        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length(); i++) {
                VipData vipData = new VipData();
                DecimalFormat percent = new DecimalFormat("0.00");
                DecimalFormat df = new DecimalFormat("0.00%");
                vipData.setData(jsonArray.getJSONArray(i).getString(0));
                vipData.setQty(jsonArray.getJSONArray(i).getString(1));
                vipData.setRateIo(percent.format(jsonArray.getJSONArray(i).getDouble(2)));
                vipData.setRate(df.format(jsonArray.getJSONArray(i).getDouble(2)));
                vList.add(vipData);
            }
        }
        return vList;
    }

    private MultiValueMap Data(String storeId) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("NAME");//星座
        colums.add("QTY");//人数
        colums.add("RATIO");//占比

        params.put("columns", colums);
        params.put("table", "CONSTELLATION_RATIO_STORE");//表名
        params.put("count", "true");

        HashMap paramss = new HashMap();

        paramss.put("column","'C_STORE_ID'");
        String a = "'"+storeId+"'";
        paramss.put("condition",a);

//        HashMap expr2 = new HashMap();
//        String ac =  "'"+startDate+"~"+endDate+"'";
//        expr2.put("column", "'M_RETAIL_ID;BILLDATE'");
//        expr2.put("condition", ac);

//        HashMap orderby = new HashMap();
//
//        orderby.put("column", "BILLDATE");
//        orderby.put("asc", "true");
//        ArrayList list = new ArrayList();
//        list.add(orderby);
//
//        params.put("orderby", list);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getConstellation g = new getConstellation("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("3128"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
